package ink.lovejinhu.leetcode;

import ink.lovejinhu.common.TreeNode;

public class Solution_0572 {

    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
        return this.dfs(root, subRoot);
    }

    public boolean dfs(TreeNode root, TreeNode subroot) {
        if (root == null && subroot == null) {
            return true;
        }
        return checkEqual(root, subroot) || dfs(root.left, subroot) || dfs(root.right, subroot);
    }

    public boolean checkEqual(TreeNode root, TreeNode subRoot) {
        if (root == null && subRoot == null) {
            return true;
        }
        if (root == null || subRoot == null) {
            return false;
        }
        if (root.val == subRoot.val) {
            return checkEqual(root.left, subRoot.left) && checkEqual(root.right, subRoot.right);
        } else {
            return false;
        }

    }
}
